package com.jlastu.znb.adapter;


import static android.view.View.GONE;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jlastu.znb.R;
import com.jlastu.znb.sc.scItem;

import java.util.List;
import java.util.Locale;

public class scAdapter extends ArrayAdapter<scItem> {
    private Context context;
    private List<scItem> scItems;

    public scAdapter(Context context, List<scItem> scItems) {
        super(context, R.layout.sc_item, scItems);
        this.context = context;
        this.scItems = scItems;
//        super(context, 0, scItems);
//        this.context = context;
//        this.scItems = scItems;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 复用convertView以优化性能
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.sc_item, parent, false);
        }

        scItem scItem = scItems.get(position);
        ImageView itemImageView = convertView.findViewById(R.id.imageViewProduct1);
        TextView itemNameView = convertView.findViewById(R.id.sc_item_name1);
        TextView itemPriceView = convertView.findViewById(R.id.sc_item_price1);
        TextView itemQuantityView = convertView.findViewById(R.id.sc_item_quantity1);
        Bitmap bitmap = scItem.getBitmap();
        if (!scItem.hasImage()) {
            convertView.setVisibility(GONE);
            itemImageView.setVisibility(GONE);
        } else {
            convertView.setVisibility(View.VISIBLE);
            itemImageView.setImageBitmap(scItem.getBitmap());
        }
        if (itemImageView != null && bitmap != null) {
            itemImageView.setImageBitmap(bitmap);
        } else if (itemImageView != null ){
            itemImageView.setImageResource(R.drawable.xiandou);
        }
        itemNameView.setText(scItem.getGoodsName());
        itemPriceView.setText(String.format(Locale.getDefault(), "¥ %.2f", scItem.getGoodsPrice()));
        itemQuantityView.setText(String.valueOf(scItem.getQuantity()));

        return convertView;
    }
}